package com.wtf.dsp.admin.config;

import com.wtf.dsp.admin.filter.LogMdcFilter;
import com.wtf.dsp.admin.interceptor.PermissionInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

/**
 * ClassName: PermissionInterceptor
 * Description:
 * date: 2024/3/6 18:16
 *
 * @author linhuanyou
 * @version 1.0
 */
@Slf4j
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    @Autowired
    private PermissionInterceptor permissionInterceptor;

    @Value("${wtf.dsp.permit.urls}")
    private List<String> premitUrls;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        log.info("wtf.dsp.permit.urls : {}",premitUrls);
        registry.addInterceptor(permissionInterceptor)
                .excludePathPatterns(premitUrls)
                .addPathPatterns("/**");
    }


    @Bean
    public FilterRegistrationBean<LogMdcFilter> logFilterRegistration() {
        FilterRegistrationBean<LogMdcFilter> registration = new FilterRegistrationBean<>();
        // 注入过滤器
        registration.setFilter(new LogMdcFilter());
        // 拦截规则
        registration.addUrlPatterns("/*");
        // 过滤器名称
        registration.setName("LogMdcFilter");
        // 过滤器顺序
        registration.setOrder(0);
        return registration;
    }
}